package com.bdqn.service.impl;

import com.bdqn.entity.Dish;
import com.bdqn.entity.DishFlavor;
import com.bdqn.mapper.DishFlavorMapper;
import com.bdqn.mapper.DishMapper;
import com.bdqn.param.response.DishDto;
import com.bdqn.param.response.Page;
import com.bdqn.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author hunter
 * @since 2022-06-11
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
   @Autowired
   private DishMapper dishMapper;
   @Autowired
   private DishFlavorMapper dishFlavorMapper;
    @Override
    public Page<DishDto> pageInfo(Integer page, Integer pageSize,String name) {
        PageHelper.startPage(page,pageSize);
        PageInfo<DishDto> pageInfo=new PageInfo<DishDto>(dishMapper.selectDishList(name));

        //封装分页对象
        return new Page<DishDto>(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 通过菜品id 查询菜品的详情信息
     * @param id
     * @return
     */
    @Override
    public DishDto dishDto(Long id) {
        //通过id查询dish菜品信息
        Dish dish = dishMapper.selectById(id);
        //对应的口味集合
         List<DishFlavor> dishFlavorList= dishFlavorMapper.selectByDishId(id);

        return new DishDto(dish,dishFlavorList,"");
    }

    /**
     * 通过菜品分类id 获取该分类下的菜品及其口味集合
     * @param categoryId
     * @return
     */
    @Override
    public List<DishDto> getDishByCategory(Long categoryId) {
        //获取菜品信息
        List<Dish> dishByCategory = dishMapper.getDishByCategory(categoryId);
        //此集合存放菜品对应的口味
        List<DishDto> dishDto = new ArrayList<>();
        for (Dish dish : dishByCategory) {
            DishDto dishDto1=new DishDto();
            dishDto1.setId(dish.getId());
            dishDto1.setCategoryId(dish.getCategoryId());
            dishDto1.setStatus(dish.getStatus());
            dishDto1.setCode(dish.getCode());
            dishDto1.setDescription(dish.getDescription());
            dishDto1.setImage(dish.getImage());
            dishDto1.setPrice(dish.getPrice());
            dishDto1.setSort(dish.getSort());
            dishDto1.setName(dish.getName());
            dishDto1.setFlavors(dishFlavorMapper.selectByDishId(dish.getId()));
            dishDto.add(dishDto1);
        }
         
        return dishDto;
    }
}
